/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-04-22
 * Time: 22:15
 */

import java.io.File;
import java.util.Scanner;

/**
 * 根据关键词,寻找文件
 */
public class Demo1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要查询的目录");
        String rootPath = scanner.next();
        System.out.println("请输入要查找的词");
        String searchWord = scanner.next();
        File rootFile = new File(rootPath);
        if(!rootFile.isDirectory()){
            System.out.println("输入的不是目录");
            return;
        }
        search(rootFile,searchWord);
    }
    public static void search(File file,String searchWord){
        File[] files = file.listFiles();
        if(files==null){
            return;
        }
        for (File f:files) {
            if(f.isFile()){
                if(f.getName().contains(searchWord)){
                    System.out.println(f.getAbsolutePath());
                    System.out.println("是否要删除该文件:");
                    System.out.println("0 : 删除  ; 1 : 保留");
                    Scanner scanner =new Scanner(System.in);
                    int n = scanner.nextInt();
                    if(n==0){
                        f.delete();
                        System.out.println(f.exists());
                    }
                }
            }else if(f.isDirectory()){
                search(f,searchWord);
            }
        }
    }
}
